#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int fd;
    int flag;
    int ret = 0;

    fd = open(argv[1], O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    if(-1 == fd){
        perror("open error");
        exit(-1);
    }

    flag = fcntl(fd, F_GETFL);
    if(-1 == flag){
        perror("fcntl F_GETFL error");
        ret = -1;
        goto err;
    }

    printf("flag: 0x%x\n", flag);

    ret = fcntl(fd, F_SETFL,flag|O_APPEND);
    if(-1 == ret){
        perror("fcntl F_SETFL error");
        goto err;
    }

    err:
        close(fd);
        exit(ret);

    close(fd);
    exit(ret);
}
